package com.itheima.infoManager.staticDemo.manager.service;

import com.itheima.infoManager.staticDemo.manager.dao.StudentDao;
import com.itheima.infoManager.staticDemo.manager.domain.Student;

public class StudentService {
    private StudentDao dao = new StudentDao();
    //添加
    public boolean addStudent(Student stu) {
        //service层没有数据操作权限，所以将数据向下传递
        return dao.addStudent(stu);//将controller层的请求，向下提交，让dao层完成数据添加操作
    }

    //判断学号是否存在重复
    public boolean isExists(String id) {
        Student[] stus = dao.findAllStudent();
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            //判断数组中的Student对象的id属性是否有和当前id一样的
            if (stu!=null && stu.getId().equals(id)){
                return true;
            }
        }
        return false;
    }

    //查看
    public Student[] findAllStudent() {
        //拿到数组
        Student[] allStudent = dao.findAllStudent();
        //遍历
        for (int i = 0; i < allStudent.length; i++) {
            Student stu = allStudent[i];
            //如果数组内有值，则将数组返回
            if (stu!=null){
                return allStudent;
            }
        }
        //没有值则返回null，交给上一层去判断
        return null;
    }

    //删除
    public boolean deleteStudent(String delId) {
        return dao.deleteStudent(delId);
    }

    public boolean updateStudent(String upId, Student student) {
        return dao.updateStudent(upId,student);
    }
}
